package com.financial.business.yspay.mq;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.financial.business.yspay.service.FinancialYsCallBackService;
import com.financial.business.yspay.utils.JsonUtils;
import com.financial.business.yspay.utils.apiresult.ApiResult;
import com.financial.business.yspay.utils.apiresult.ApiResultCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;


/**
 * @Author WANGNING
 * @Email wyn20140226@aliyun.com
 * @Description //消息处理类
 * @Date 2019/11/14 11:11
 */
@Component
public class FinancialYsMessageListener implements MessageListener {

    public static final Logger logger = LoggerFactory.getLogger(FinancialYsMessageListener.class);
    @Autowired
    private FinancialYsCallBackService financialYsCallBackService;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        logger.error(new Date() + "FinancialYsMessageListener Receive message, Topic is:" + message.getTopic()
                + ", MsgId is:" + message.getMsgID() + ",tags is:" + message.getTag());
        logger.error("FinancialYsMessageListener-->consume topic:" + message.getTag() + "，MsgId:" + message.getMsgID() + "-----message:" + new String(message.getBody()));
        if (message.getTopic().equals(FinancialYsMQConfig.TOPIC)) {
            /*获取消息重试次数*/
            int reconsume = message.getReconsumeTimes();
            if (reconsume == 3) {
                /*消息已经重试了3次，告诉服务器稍后在尝试消费*/
                logger.error("FinancialYsMessageListener-->consume topic:" + message.getTag() + "，MsgId:" + message.getMsgID() + " is already try 3 times");
                return Action.CommitMessage;
            }
            if (StringUtils.isNotEmpty(message.getTag()) && FinancialYsMQConfig.YS_PAY_WECHAT_TAG.equals(message.getTag())) {
                logger.error("FinancialYsMessageListener-->wechat--->" + FinancialYsMQConfig.YS_PAY_WECHAT_TAG);
                /*银盛微信支付异步回执*/
                try {
                    Map body = JsonUtils.jsonToMap(new String(message.getBody()));
                    logger.error("FinancialYsMessageListener-->wechat--->body:" + body);
                    /*解析消息体*/
                    String receipt = new String(message.getBody());
                    ApiResult ysWeChatReceipt = financialYsCallBackService.ysWeChatPayReceipt(receipt);
                    if (ApiResultCode.SUCCESS.code().equals(ysWeChatReceipt.getCode())) {
                        logger.error("FinancialYsMessageListener-->wechat--->consume topic:" + message.getTag() + "，MsgId:" + message.getMsgID() + "consum commit");
                    }
                } catch (Exception e) {
                    logger.error("FinancialYsMessageListener-->wechat---> fail: ", e);
                }
                /*解析消息体*/
                return Action.CommitMessage;
            } else if (StringUtils.isNotEmpty(message.getTag()) && FinancialYsMQConfig.YS_PAY_ALIPAY_TAG.equals(message.getTag())) {
                /*银盛支付宝支付*/
                logger.error("FinancialYsMessageListener-->alipay--->" + FinancialYsMQConfig.YS_PAY_ALIPAY_TAG);
                try {
                    Map body = JsonUtils.jsonToMap(new String(message.getBody()));
                    logger.error("FinancialYsMessageListener-->alipay--->body:" + body);
                    /*解析消息体*/
                    String receipt = new String(message.getBody());
                    ApiResult ysAlipayReceipt = financialYsCallBackService.ysAliPayReceipt(receipt);
                    if (ApiResultCode.SUCCESS.code().equals(ysAlipayReceipt.getCode())) {
                        logger.error("FinancialYsMessageListener-->alipay--->consume topic:" + message.getTag() + "，MsgId:" + message.getMsgID() + "consum commit");
                    }
                } catch (Exception e) {
                    logger.error("FinancialYsMessageListener-->alipay---> fail: ", e);
                }
            } else if (StringUtils.isNotEmpty(message.getTag()) && FinancialYsMQConfig.YS_SERVICE_SPLIT_TAG.equals(message.getTag())) {
                /*银盛分账*/
                logger.error("FinancialYsMessageListener-->split--->" + FinancialYsMQConfig.YS_SERVICE_SPLIT_TAG);
                try {
                    Map body = JsonUtils.jsonToMap(new String(message.getBody()));
                    logger.error("FinancialYsMessageListener-->split--->body:" + body);
                    /*解析消息体*/
                    String receipt = new String(message.getBody());
                    ApiResult ysSplitReceipt = financialYsCallBackService.ysSplitReceipt(receipt);
                    if (ApiResultCode.SUCCESS.code().equals(ysSplitReceipt.getCode())) {
                        logger.error("FinancialYsMessageListener-->split--->consume topic:" + message.getTag() + "，MsgId:" + message.getMsgID() + "consum commit");
                    }
                } catch (Exception e) {
                    logger.error("FinancialYsMessageListener-->split---> fail: ", e);
                }
            } else if (StringUtils.isNotEmpty(message.getTag()) && FinancialYsMQConfig.YS_SERVICE_WITHDRAWAL_TAG.equals(message.getTag())) {
                /*银盛提现*/
                logger.error("FinancialYsMessageListener-->withdrawal--->" + FinancialYsMQConfig.YS_SERVICE_WITHDRAWAL_TAG);
                try {
                    Map body = JsonUtils.jsonToMap(new String(message.getBody()));
                    logger.error("FinancialYsMessageListener-->withdrawal--->body:" + body);
                    /*解析消息体*/
                    String receipt = new String(message.getBody());
                    ApiResult ysWithdrawalReceipt = financialYsCallBackService.ysWithdrawalReceipt(receipt);
                    if (ApiResultCode.SUCCESS.code().equals(ysWithdrawalReceipt.getCode())) {
                        logger.error("FinancialYsMessageListener-->withdrawal--->consume topic:" + message.getTag() + "，MsgId:" + message.getMsgID() + "consum commit");
                    }
                } catch (Exception e) {
                    logger.error("FinancialYsMessageListener-->withdrawal---> fail: ", e);
                }
            }else if (StringUtils.isNotEmpty(message.getTag()) && FinancialYsMQConfig.YS_SERVICE_PAYMENT_TAG.equals(message.getTag())) {
                /*银盛代付*/
                logger.error("FinancialYsMessageListener-->payment" + FinancialYsMQConfig.YS_SERVICE_PAYMENT_TAG);
                try {
                    Map body = JsonUtils.jsonToMap(new String(message.getBody()));
                    logger.error("FinancialYsMessageListener-->payment--->body:" + body);
                    /*解析消息体*/
                    String receipt = new String(message.getBody());
                    ApiResult ysWithdrawalReceipt = financialYsCallBackService.ysWithdrawalReceipt(receipt);
                    if (ApiResultCode.SUCCESS.code().equals(ysWithdrawalReceipt.getCode())) {
                        logger.error("FinancialYsMessageListener-->payment--->consume topic:" + message.getTag() + "，MsgId:" + message.getMsgID() + "consum commit");
                    }
                } catch (Exception e) {
                    logger.error("FinancialYsMessageListener-->payment---> fail: ", e);
                }
            }
        }
        logger.error("MessageListenerImpl-->consume topic:" + message.getTag() + "，MsgId:" + message.getMsgID() + "consum fail");
        return Action.CommitMessage;
        /*如果想测试消息重投的功能,可以将Action.CommitMessage 替换成Action.ReconsumeLater*/
    }

}
